package com.lebang.controller.sys.cences;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.domain.CencesHundredDownloadRecord;
import com.lebang.example.CencesHundredDownloadRecordExample;
import com.lebang.feign.HundredAwardsFeign;
import com.lebang.model.RequestUserInfo;
import com.lebang.model.cences.DoubleConfig;
import com.lebang.service.cences.CencesHundredDownloadRecordService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * 公交每日必赚统计
 *
 * @author ljx
 * @date 2020-12-03
 */
@Controller
@RequestMapping("/sys/cenceshundreddownloadrecord")
public class CencesHundredDownloadRecordController {
    @Autowired
    private CencesHundredDownloadRecordService baseService;
    @Autowired
    private HundredAwardsFeign hundredAwardsFeign;

    @Login
    @GetMapping("/list")
    public String list() {
        return "/cenceshundreddownloadrecord/list";
    }

    @Login
    @GetMapping("/add")
    public String add(Integer id, Model model) {
        CencesHundredDownloadRecord vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new CencesHundredDownloadRecord();
        }
        model.addAttribute("item", vo);
        return "/cenceshundreddownloadrecord/edit";
    }


    @Login
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(CencesHundredDownloadRecord vo) {
        if (vo.getId() != null) {
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            baseService.insertModel(vo);
        }
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id,Integer time) {
        baseService.deleteRecord(time);
        return Callback.success("success", "操作成功");
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit, String appStore, Integer star, Integer end) {
        CencesHundredDownloadRecordExample example = new CencesHundredDownloadRecordExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        example.setOrderByClause("time desc,id desc");
        CencesHundredDownloadRecordExample.Criteria criteria = example.createCriteria();
        criteria.andAppStoreEqualTo(appStore);
        if (star != null && end != null) {
            criteria.andTimeBetween(star, end);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }

    @Login
    @PostMapping("/lastCheck")
    @ResponseBody
    public String cen(HttpServletRequest request, String date) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate end=LocalDate.parse(date, fmt);
        RequestUserInfo requestUserInfo=new RequestUserInfo(request);
        JsonData data = hundredAwardsFeign.doubleConfig(requestUserInfo);
        List<DoubleConfig> configList = JSONArray.parseArray(JSONObject.toJSONString(data.getData()), DoubleConfig.class);
        baseService.starDataInit(LocalDateTime.of(end.plusDays(1), LocalTime.MIN),configList);
        return "success";
    }
}
